package fun.tryto.ctp;

public class MdSpiImpl extends CThostFtdcMdSpi {
	final static String brokerID = "9999";
	final static String userID = "13910072705";
	final static String password = "Abcd1234%^&*";

	private CThostFtdcMdApi mdApi;

	MdSpiImpl(CThostFtdcMdApi mdApi) {
		this.mdApi = mdApi;
	}

	@Override
	public void OnFrontConnected() {
		System.out.println("On Front Connected");
		CThostFtdcReqUserLoginField field = new CThostFtdcReqUserLoginField();
		field.setBrokerID(brokerID);
		field.setUserID(userID);
		field.setPassword(password);
		mdApi.ReqUserLogin(field, 0);

	}

	@Override
	public void OnRspUserLogin(CThostFtdcRspUserLoginField pRspUserLogin, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		if (pRspUserLogin != null) {
			System.out.printf("Brokerid[%s]\n", pRspUserLogin.getBrokerID());
		}

		String[] instruementid = new String[1];
		instruementid[0] = "sc2401";
		mdApi.SubscribeMarketData(instruementid, 1);
	}

	@Override
	public void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField pDepthMarketData) {
		if (pDepthMarketData != null) {
			System.out.printf("AskPrice1[%f]BidPrice1[%f]\n", pDepthMarketData.getAskPrice1(), pDepthMarketData.getBidPrice1());
		} else {
			System.out.printf("NULL obj\n");
		}
	}

}
